package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ILegalAreasService;
import com.mdd.admin.validate.LegalAreasCreateValidate;
import com.mdd.admin.validate.LegalAreasUpdateValidate;
import com.mdd.admin.validate.LegalAreasSearchValidate;
import com.mdd.admin.vo.LegalAreasListedVo;
import com.mdd.admin.vo.LegalAreasDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalAreas;
import com.mdd.common.mapper.LegalAreasMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 地区实现类
 * @author LikeAdmin
 */
@Service
public class LegalAreasServiceImpl implements ILegalAreasService {
        
    @Resource
    LegalAreasMapper legalAreasMapper;

    /**
     * 地区列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalAreasListedVo>
     */
    @Override
    public PageResult<LegalAreasListedVo> list(PageValidate pageValidate, LegalAreasSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LegalAreas> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 2);
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        legalAreasMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
        });

        IPage<LegalAreas> iPage = legalAreasMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LegalAreasListedVo> list = new LinkedList<>();
        for(LegalAreas item : iPage.getRecords()) {
            LegalAreasListedVo vo = new LegalAreasListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 地区详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalAreas
     */
    @Override
    public LegalAreasDetailVo detail(Integer id) {
        LegalAreas model = legalAreasMapper.selectOne(
                new QueryWrapper<LegalAreas>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LegalAreasDetailVo vo = new LegalAreasDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 地区新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LegalAreasCreateValidate createValidate) {
        LegalAreas model = new LegalAreas();
//        model.setType(createValidate.getType());
        model.setName(createValidate.getName());
        model.setMobile(createValidate.getMobile());
        model.setCommissionedParty(createValidate.getCommissionedParty());
        model.setAddress(createValidate.getAddress());
        model.setCertificateNo(createValidate.getCertificateNo());
        model.setAccountName(createValidate.getAccountName());
        model.setAccountNumber(createValidate.getAccountNumber());
        model.setBankName(createValidate.getBankName());
//        model.setAmount(createValidate.getAmount());
        model.setStatus(createValidate.getStatus());
        model.setSort(createValidate.getSort());
        model.setAreaId(createValidate.getAreaId());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalAreasMapper.insert(model);
    }

    /**
     * 地区编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalAreasUpdateValidate updateValidate) {
        LegalAreas model = legalAreasMapper.selectOne(
                new QueryWrapper<LegalAreas>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
//        model.setType(updateValidate.getType());
        model.setName(updateValidate.getName());
        model.setMobile(updateValidate.getMobile());
        model.setCommissionedParty(updateValidate.getCommissionedParty());
        model.setAddress(updateValidate.getAddress());
        model.setCertificateNo(updateValidate.getCertificateNo());
        model.setAccountName(updateValidate.getAccountName());
        model.setAccountNumber(updateValidate.getAccountNumber());
        model.setBankName(updateValidate.getBankName());
//        model.setAmount(updateValidate.getAmount());
        model.setStatus(updateValidate.getStatus());
        model.setSort(updateValidate.getSort());
        model.setAreaId(updateValidate.getAreaId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalAreasMapper.updateById(model);
    }

    /**
     * 地区删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LegalAreas model = legalAreasMapper.selectOne(
                new QueryWrapper<LegalAreas>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        legalAreasMapper.delete(new QueryWrapper<LegalAreas>().eq("id", id));
    }

}
